home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / comm / tcp / Amster-source.lha / Amster_Install / Source / rexx.c < prev    next >
C/C++ Source or Header  |  2001-03-11  |  7KB  |  268 lines

  1. /*
  2. ** Amster - ARexx support
  3. ** Copyright © 1999-2000 by Gürer Özen
  4. ** Copyright © 2000-2001 by Jacob Laursen
  5. **
  6. ** This program is free software; you can redistribute it and/or modify
  7. ** it under the terms of the GNU General Public License as published by
  8. ** the Free Software Foundation; either version 2 of the License, or
  9. ** (at your option) any later version.
  10. **
  11. ** This program is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ** GNU General Public License for more details.
  15. **
  16. ** You should have received a copy of the GNU General Public License
  17. ** along with this program; if not, write to the Free Software
  18. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19. */
  20.  
  21. #include "amster.h"
  22.  
  23. #include <proto/exec.h>
  24. #include <proto/dos.h>
  25. #include <proto/rexxsyslib.h>
  26. #include <rexx/storage.h>
  27.  
  28. #include "amster_Cat.h"
  29.  
  30. MUI_HOOK_DECL(rexx_con, Object *app, char **array);
  31. MUI_HOOK_DECL(rexx_dis, Object *app, char **array);
  32. MUI_HOOK_DECL(rexx_ison, Object *app, char **array);
  33. MUI_HOOK_DECL(rexx_search, Object *app, char **array);
  34. MUI_HOOK_DECL(rexx_sstat, Object *app, char **array);
  35. MUI_HOOK_DECL(rexx_whois, Object *app, char **array);
  36. MUI_HOOK_DECL(rexx_download, Object *app, char **array);
  37. MUI_HOOK_DECL(rexx_serverstat, Object *app, char **array);
  38. MUI_HOOK_DECL(rexx_window, Object *app, char **array);
  39.  
  40. struct Hook rexx_conHook = { {0,0}, &rexx_con, NULL, NULL };
  41. struct Hook rexx_disHook = { {0,0}, &rexx_dis, NULL, NULL };
  42. struct Hook rexx_isonHook = { {0,0}, &rexx_ison, NULL, NULL };
  43. struct Hook rexx_searchHook = { {0,0}, &rexx_search, NULL, NULL };
  44. struct Hook rexx_sstatHook = { {0,0}, &rexx_sstat, NULL, NULL };
  45. struct Hook rexx_whoisHook = { {0,0}, &rexx_whois, NULL, NULL };
  46. struct Hook rexx_downloadHook = { {0,0}, &rexx_download, NULL, NULL };
  47. struct Hook rexx_serverstatHook = { {0,0}, &rexx_serverstat, NULL, NULL };
  48. struct Hook rexx_windowHook = { {0,0}, &rexx_window, NULL, NULL };
  49.  
  50. struct MUI_Command rexx_cmds[] = {
  51.     { "connect", "SERVER/K,PORT/K/N", 2, &rexx_conHook },
  52.     { "disconnect", NULL, 0, &rexx_disHook },
  53.     { "isonline", NULL, 0, &rexx_isonHook },
  54.     { "search", "TITLE/A,STEM/A,MAXRESULTS/K/N", 3, &rexx_searchHook },
  55.     { "searchstate", NULL, 0, &rexx_sstatHook },
  56.     { "whois", "USER/A,STEM/A", 2, &rexx_whoisHook },
  57.     { "download", "NUM/A/N", 1, &rexx_downloadHook },
  58.     { "getserverstats", "STEM/A", 1, &rexx_serverstatHook },
  59.     { "window", "NAME/A,OPEN/S,CLOSE/S", 3, &rexx_windowHook },
  60.     { NULL, NULL, 0, NULL }
  61. };
  62.  
  63. ULONG gRC;
  64. enum RexxCommand gRexxCommand = NONE;
  65. char *gSTEM;
  66.  
  67.  
  68. MUI_HOOK(rexx_con, Object *app, char **array)
  69. {
  70.     if (gui_onlinestate > OFFLINE) nap_logout();
  71.     if (array[0] == NULL || array[1] == NULL) nap_login();
  72.     else nap_login_fromlist(array[0], *(long *)array[1]);
  73.  
  74.     return(0);
  75. }
  76.  
  77.  
  78. MUI_HOOK(rexx_dis, Object *app, char **array)
  79. {
  80.     nap_logout();
  81.     set(gui->stat, MUIA_Text_Contents, MSG_STATUS2_NOTCONNECTED);
  82.     return(0);
  83. }
  84.  
  85.  
  86. MUI_HOOK(rexx_ison, Object *app, char **array)
  87. {
  88.     if (gui_onlinestate == ONLINE) return 1;
  89.     else return 0;
  90. }
  91.  
  92.  
  93. MUI_HOOK(rexx_search, Object *app, char **array)
  94. {
  95.     struct SearchArgs args;
  96.  
  97.     gRC = 0;
  98.     gRexxCommand = SEARCH;
  99.  
  100.     if (gui_onlinestate < ONLINE) return 15;
  101.  
  102.     args.String = array[0];
  103.     if (array[2]) args.MaxResults = *(long *)array[2];
  104.     else args.MaxResults = -1;
  105.  
  106.     DoMethod(gui->searchpanel, SEARCH_GO, &args);
  107.     DoMethod(gui->searchpanel, SEARCH_GETSTATE);
  108.     if (search_state == 1) do {
  109.         nap_listen();
  110.         DoMethod(gui->searchpanel, SEARCH_GETSTATE);
  111.     } while (search_state == 1);
  112.  
  113.     if (search_state == 2) DoMethod(gui->searchpanel, SEARCH_FILLSTEM, array[1]);
  114.     else gRC = 10;
  115.     gRexxCommand = NONE;
  116.  
  117.     return gRC;
  118. }
  119.  
  120.  
  121. MUI_HOOK(rexx_sstat, Object *app, char **array)
  122. {
  123.     DoMethod(gui->searchpanel, SEARCH_GETSTATE);
  124.     return (ULONG)search_state;
  125. }
  126.  
  127.  
  128. MUI_HOOK(rexx_whois, Object *app, char **array)
  129. {
  130.     if (gui_onlinestate < ONLINE) return 10;
  131.  
  132.     gRC = 0;
  133.     gRexxCommand = WHOIS;
  134.     gSTEM = array[1];
  135.  
  136.     nap_sendbuf(NAPC_WHOIS, array[0]);
  137.     do {
  138.         nap_listen();
  139.     } while (gRexxCommand == WHOIS);
  140.  
  141.     return gRC;
  142. }
  143.  
  144.  
  145. MUI_HOOK(rexx_download, Object *app, char **array)
  146. {
  147.     gRC = 0;
  148.  
  149.     DoMethod(gui->searchpanel, SEARCH_DOWNLOAD, *(long *)array[0]);
  150.  
  151.     return gRC;
  152. }
  153.  
  154.  
  155. MUI_HOOK(rexx_serverstat, Object *app, char **array)
  156. {
  157.     struct RexxMsg *m;
  158.     u_long tmp;
  159.     char buf1[64], buf2[64];
  160.  
  161.     GetAttr(MUIA_Application_RexxMsg, gui->app, &tmp);
  162.     m = (struct RexxMsg *)tmp;
  163.  
  164.     sprintf(buf1, "%s.USERS", array[0]);
  165.     sprintf(buf2, "%ld", gui->Libraries);
  166.     SetRexxVar(m, buf1, buf2, strlen(buf2));
  167.  
  168.     sprintf(buf1, "%s.SONGS", array[0]);
  169.     sprintf(buf2, "%ld", gui->Songs);
  170.     SetRexxVar(m, buf1, buf2, strlen(buf2));
  171.  
  172.     sprintf(buf1, "%s.GIGABYTES", array[0]);
  173.     sprintf(buf2, "%ld", gui->Gigabytes);
  174.     SetRexxVar(m, buf1, buf2, strlen(buf2));
  175.  
  176.     sprintf(buf1, "%s.SERVER", array[0]);
  177.     SetRexxVar(m, buf1, nap_server, strlen(nap_server));
  178.  
  179.     sprintf(buf1, "%s.PORT", array[0]);
  180.     sprintf(buf2, "%d", nap_port);
  181.     SetRexxVar(m, buf1, buf2, strlen(buf2));
  182.  
  183.     sprintf(buf1, "%s.CONNECTTIME", array[0]);
  184.     sprintf(buf2, "%ld", gui->OnlineSince);
  185.     SetRexxVar(m, buf1, buf2, strlen(buf2));
  186.  
  187.     if (gui_onlinestate < ONLINE) return 5;
  188.     else return 0;
  189. }
  190.  
  191.  
  192. MUI_HOOK(rexx_window, Object *app, char **array)
  193. {
  194.     if (stricmp(array[0], "upload") == 0) {
  195.         if (array[1]) DoMethod(gui->uwin, UPLOAD_OPEN);
  196.         else if (array[2]) DoMethod(gui->uwin, UPLOAD_CLOSE);
  197.     }
  198.     else if (stricmp(array[0], "download") == 0) {
  199.         if (array[1]) DoMethod(gui->iconpanel, PANEL_OPENDL);
  200.         else if (array[2]) DoMethod(gui->iconpanel, PANEL_CLOSEDL);
  201.     }
  202.     else if (stricmp(array[0], "debug") == 0) {
  203.         if (array[1]) DoMethod(gui->iconpanel, PANEL_OPENDEBUG);
  204.         else if (array[2]) DoMethod(gui->iconpanel, PANEL_CLOSEDEBUG);
  205.     }
  206.     else if (stricmp(array[0], "library") == 0) {
  207.         if (array[1]) DoMethod(gui->iconpanel, PANEL_OPENSHARE);
  208.         else if (array[2]) DoMethod(gui->iconpanel, PANEL_CLOSESHARE);
  209.     }
  210.     else if (stricmp(array[0], "hotlist") == 0) {
  211.         if (array[1]) set(gui->WI_Hotlist, MUIA_Window_Open, TRUE);
  212.         else if (array[2]) set(gui->WI_Hotlist, MUIA_Window_Open, FALSE);
  213.     }
  214.     else if (stricmp(array[0], "message") == 0) {
  215.         if (array[1]) set(gui->mwin, MUIA_Window_Open, TRUE);
  216.         else if (array[2]) set(gui->mwin, MUIA_Window_Open, FALSE);
  217.     }
  218.  
  219.     return 0;
  220. }
  221.  
  222.  
  223. u_long rexx_sendcommand(char *port, char *com)
  224. {
  225.     struct MsgPort *me,*him;
  226.     struct RexxMsg *rmsg;
  227.     u_long rc=0;
  228.  
  229.     if (me = CreateMsgPort()) {
  230.         if (rmsg = CreateRexxMsg(me,NULL,NULL)) {
  231.             if (rmsg->rm_Args[0] = CreateArgstring(com, strlen(com))) {
  232.                 rmsg->rm_Action = RXCOMM;
  233.                 Forbid();
  234.                 him = FindPort(port);
  235.                 Permit();
  236.                 if (him) {
  237.                     PutMsg(him, (struct Message *)rmsg);
  238.                     WaitPort(me);
  239.                     GetMsg(me);
  240.                     rc = rmsg->rm_Result1;
  241. /*                    if (!rc && rmsg->rm_Result2) DeleteArgstring(rmsg->rm_Result2);*/
  242.                 }
  243.                 DeleteArgstring(rmsg->rm_Args[0]);
  244.             }
  245.             DeleteRexxMsg(rmsg);
  246.         }
  247.         DeleteMsgPort(me);
  248.     }
  249.     return rc;
  250. }
  251.  
  252.  
  253. void rexx_execute(char *com, char *argfmt, ...)
  254. {
  255.     static char buf[1024];
  256.     char *p;
  257.     va_list ap;
  258.  
  259.     p = buf + sprintf(buf,"Run <>NIL: RexxC:RX %s ",com);
  260.     if(argfmt) {
  261.         va_start(ap,argfmt);
  262.         vsprintf(p,argfmt,ap);
  263.         va_end(ap);
  264.     }
  265.  
  266.     Execute(buf, 0, 0);
  267. }
  268.